package com.xingdata.jjxc.m.avt;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 用户常用地址增-修改20150413
 * 
 * @author maomao
 * 
 */
public class ComAddressSetavt extends BaseActivity implements OnClickListener {

	private EditText com_address_set_et;
	private ImageView com_address_set_iv;
	private Button com_address_set_btn;

	private String areaid;
	private String address;
	private String longitude;
	private String latitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_com_address_set;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_COMADDRESSSET;
	}

	@Override
	protected void initViews() {
		com_address_set_et = (EditText) findViewById(R.id.com_address_set_et);
		com_address_set_iv = (ImageView) findViewById(R.id.com_address_set_iv);
		com_address_set_btn = (Button) findViewById(R.id.com_address_set_btn);
		com_address_set_iv.setOnClickListener(this);
		com_address_set_btn.setOnClickListener(this);

		com_address_set_et.setText(areaid);
	}

	@Override
	public void onClick(View v) {
		String com_placename = com_address_set_et.getText().toString();
		switch (v.getId()) {
		case R.id.com_address_set_iv:
			com_address_set_et.setText("");
			break;
		case R.id.com_address_set_btn:
			if ("".equals(com_placename)) {
				showToast("请输入常用地点名称");
			} else {
				doPost_changeName(com_placename, areaid, address, longitude,
						latitude);
			}

			break;

		default:
			break;
		}

	}

	@Override
	protected void initDateBeforSetContentView() {
		Bundle bundle = getIntent().getExtras();
		areaid = bundle.getString("areaid");
		address = bundle.getString("address");
		longitude = bundle.getString("longitude");
		latitude = bundle.getString("latitude");
	}

	private void doPost_changeName(String nick_address, String areaid,
			String address, String longitude, String latitude) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		resp = null;
		params = new AbRequestParams();
		params.put("access_token", Code.accessToken());
		params.put("userid", SystemInfo.getUserInfo().getUserid());
		params.put("nick_address", nick_address);
		params.put("areaid", areaid);
		params.put("address", address);
		params.put("longitude", longitude);
		params.put("latitude", latitude);
		params.put("maptype", "2");//为高德地图
		params.put("flag", "1");

		mAbHttpUtil.post(App.ZZD_REQUEST_CHANGENAME, params,
				new AbStringHttpResponseListener() {
					@Override
					public void onSuccess(int statusCode, String content) {
						resp = JSON.parseObject(content, RespEntity.class);
					}

					@Override
					public void onFailure(int statusCode, String content,
							Throwable error) {
					}

					@Override
					public void onStart() {
						super.onStart();
						showProgressDialog("");
					}

					@Override
					public void onFinish() {
						if (resp != null) {
							if (resp.getState() == 0) {
								showToast("备注名称保存成功");
								startActivity(new Intent(ComAddressSetavt.this,
										Commonaddavt.class));
								finish();

							} else if (resp.getState() == 1) {
								showToast(resp.getMsg());
							} else {
								showToast(resp.getMsg());
							}
							removeProgressDialog();
						} else {
							showToast("网络超时，请重试");
							removeProgressDialog();
							return;
						}
					}
				});
	}

}
